package pms.common.dao;

import pms.common.value.MasterManageValue;
import pms.common.value.MasterValue;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

public class MasterManageDaoImpl implements MasterManageDao {
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public List<MasterManageValue> getMasterList() {
		final List<MasterManageValue> result = new ArrayList<MasterManageValue>();
		String sql = "SELECT master_id, master_group, master_value FROM tbl_master order by master_id";
		
		jdbcTemplate.query(sql, new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				MasterManageValue mmValue = new MasterManageValue();
				mmValue.setDbCode(rs.getString("master_id"));
				mmValue.setKeyWord(rs.getString("master_group"));
				mmValue.setDisValue(rs.getString("master_value"));
				result.add(mmValue);
			}
		});
		if(result.size()==0)
			return null;
		else
			return result;
	}
	public void doUpdate(MasterManageValue l_masterManageValue) {
		String sql = "update tbl_master set master_group = ?, master_value = ? where master_id = ?";
		
		jdbcTemplate.update(sql, new Object[]{l_masterManageValue.getKeyWord(), l_masterManageValue.getDisValue(), l_masterManageValue.getDbCode()});
	}
	public void doDelete(MasterManageValue l_masterManageValue) {
		String sql = "delete from tbl_master where master_id = ?";
		
		jdbcTemplate.update(sql, new Object[]{l_masterManageValue.getDbCode()});
	}
	public void doAdd(MasterManageValue l_masterManageValue) {
		String sql = "insert into tbl_master(master_group, master_value) values(?, ?)";
		
		jdbcTemplate.update(sql, new Object[]{l_masterManageValue.getKeyWord(), l_masterManageValue.getDisValue()});
	}

}
